<template>
  <div>
    <BreakSpace label="调查要求" :containerStyle="{ marginBottom: '15px' }" />
    <FormPanel
      ref="formPanel"
      :list="formList"
      labelWidth="100"
      :formType="type"
      :isSubmitBtn="false"
      @formChange="formChangeHandle"
    />
    <BreakSpace label="故障信息" :containerStyle="{ marginBottom: '15px' }" />
      <!-- table -->
      <FilterTable
        ref="table"
        :columns="columns"
        :fetchapi="fetchApi"
        :params="fetchParams"
        :datakey="'records'"
        :isSelectColumn="false"
        :onRowSelectChange="selectChange"
        :onColumnsChange="columns => this.columns = columns"
      >
      </FilterTable>
    <div class="dialogBottmView">
<!--      <el-button @click="saveDialog(1)" type="primary">确 认</el-button>-->
      <el-button @click="closeDialog">返 回</el-button>
    </div>
  </div>
</template>

<script>
  import { dictionary } from '@/utils/dictMixin';
  import { getFileGroup } from '@/api/partsmanage/common/file';
  import moment from 'moment'
  import {insertReport,getListByRetailNo,selectQualityReport,transmissionData,updateIsDelivery} from '@/api/customerService/secneExamine/secneLssue/index';
  export default {
    name: "updateCarSceneQuery",
    mixins: [dictionary],
    props: {
      type: {
        type: String,
        default: 'add',
      },
      rowData: {
        type: Object,
        default: () => ({})
      }
    },
    data() {
      this.saveData = null;
      return {
        columns: this.createTableColumns(),
        formList: this.createFormList(),
        fetchApi: selectQualityReport,
        show: false,
        fetchParams: {
          retainNo: '',
          xhrAbort: true
        },
      };
    },
   async created() {
      this.fetchParams.retainNo = this.rowData.retainNo
      await this.init();
      this.fetchParams.xhrAbort= false
    },
    methods: {
      handlePreview(url){
        console.info(url)
        window.open(url);
        this.$message({
          type: 'success',
          message: '下载成功!'
        });
      },
      selectChange(val) {
        console.log('val', val);
        this.rowSelectData = val;
      },
      async init() {
        this.rowData.endDate= moment(this.rowData.endDate).format('YYYY-MM-DD HH:mm:ss')
        this.rowData.beginDate= moment(this.rowData.beginDate).format('YYYY-MM-DD HH:mm:ss')
        this.rowData.applyDate= moment(this.rowData.applyDate).format('YYYY-MM-DD HH:mm:ss')
        console.info(this.type)
        console.info(this.rowData)
        this.formList.forEach(x => {
          x.initialValue = this.rowData[x.fieldName];
        });
        await getFileGroup({ "xtdjid": this.rowData.id, "xtfjlx": "3" }).then(response => {
          console.info(response)
          var fileList = response.data.records
          this.formList.forEach(x => {
            if (x.fieldName === 'wayFiles') {
              x.initialValue = fileList
            }
          })
        })
        console.info(this.rowData.retainNo)
        let res = await getListByRetailNo({retailNo: this.rowData.retainNo})
        console.info(res)
        console.info(res.data)
        console.info(res.data.length)
        if (res.data.length != 0) {
          res.data.forEach(item => {
            item.retainNo = this.rowData.retainNo
            item.reportDate = item.createdAt
            item.isDelivery = 10041002
          })
          await insertReport(res.data)
        }
      },
      createFormList() {
        return [
          {
            type: 'INPUT',
            label: '留车单号',
            fieldName: 'retainNo',
            disabled: true,
          },
          {
            type: 'INPUT',
            label: '车型',
            fieldName: 'model',
            disabled: true,
          },
          {
            type: 'SELECT',
            label: '状态',
            fieldName: 'state',
            itemList:this.createDictList('5008'),
            initialValue: '',
            disabled: true,
          },
          {
            type: 'INPUT',
            label: '配件代码',
            fieldName: 'partCode',
            disabled: true,
          },
          {
            type: 'INPUT',
            label: '配件名称',
            fieldName: 'partName',
            disabled: true,
          },
          {
            type: 'INPUT',
            label: '申请人',
            fieldName: 'applicant',
            disabled: true,
          },
          {
            type: 'DATE',
            label: '申请时间',
            fieldName: 'applyDate',
            format: 'yyyy-MM-dd HH:mm:ss',
            disabled: true,
          },
          {
            type: 'INPUT',
            label: '调查范围',
            fieldName: 'scope',
            disabled: true,
          },
          {
            type: 'INPUT',
            label: 'VIN区间',
            fieldName: 'vinBetween',
            disabled: true,
          },
          {
            type: 'DATE',
            label: '生产开始时间',
            fieldName: 'beginDate',
            format: 'yyyy-MM-dd HH:mm:ss',
            disabled: true,
          },
          {
            type: 'DATE',
            label: '生产结束时间',
            fieldName: 'endDate',
            format: 'yyyy-MM-dd HH:mm:ss',
            disabled: true,
          },
          {
            type: 'INPUT',
            label: '联系人',
            fieldName: 'liaison',
            disabled: true,
          },
          {
            type: 'INPUT',
            label: '联系电话',
            fieldName: 'phone',
            disabled: true,
          },
          {
            type: 'INPUT',
            label: '故障现象',
            fieldName: 'faultPhenomenon',
            selfCols: 2,
            disabled: true,
          },
          {
            type: 'TEXT_AREA',
            selfCols: 3,
            label: '排查确认说明',
            fieldName: 'screening',
            maxlength: 200,
            disabled: true,
          },
          {
            type: 'UPLOAD_FILE',
            label: '附件',
            fieldName: 'wayFiles',
            placeholder: '附件',
            disabled: true,
            initialValue: '',
            upload: {
              actionUrl: '/api/file/oss/upload',
              limit: 5,
              fileSize: 20,
              tipText: '（支持：PDF、RAR、JPG格式）'
            }
          }
        ]
      },
      createTableColumns () {
        return[
          {
            title: '序号',
            dataIndex: 'index',
            width: 70,
            sorter: true,
            render: props => {
              return <span>{props.row.index + 1}</span>;
            }
          },
          {
            title: '质量报告单号',
            dataIndex: 'reportNo',
            sorter: true,
          },
          {
            title: 'VIN',
            dataIndex: 'vin',
            sorter: true,
          },
          {
            title: '经销商代码',
            dataIndex: 'dealerCode',
            sorter: true,
          },
          {
            title: '经销商名称',
            dataIndex: 'dealerName',
            sorter: true,
          },
          {
            title: '留车时间',
            dataIndex: 'reportDate',
            dateFormat: 'yyyy-MM-dd HH:mm:ss',
            sorter: true,
          },
          {
            title: '问题描述',
            dataIndex: 'cmsClaimInfo',
            sorter: true,
          },
          {
            title: '维修经过',
            dataIndex: 'cmsClaimProcess',
            sorter: true,
          },
          {
            title: '制单人',
            dataIndex: 'reporter',
            sorter: true,
          },
          {
            title: '联系方式',
            dataIndex: 'phone',
            sorter: true,
          },
          {
            title: '是否去现场',
            dataIndex: 'isArrive',
            sorter: true,
          },
          {
            title: '现场到达时间',
            dataIndex: 'arriveDate',
            dateFormat: 'yyyy-MM-dd HH:mm:ss',
            sorter: true,
          },
          {
            title: '调研人',
            dataIndex: 'inquirer',
            sorter: true,
          },
          {
            title: '调研电话',
            dataIndex: 'inquirerTel',
            sorter: true,
          },
          {
            title: '调查结论',
            dataIndex: 'FINDINGS',
            sorter: true,
          },
          {
            title: '附件',
            dataIndex: 'annexName',
            sorter: true,
            render: props => {
              return(
                <el-button size="mini" type="text" onClick={() => this.handlePreview(props.row.annexAddr)}>
                {props.row.annexName}
              </el-button>
              )
            }
          },
          {
            title: '是否传输成功',
            dataIndex: 'isDelivery',
            sorter: true,
            render: props => {
              return <span>{this.createDictText(props.row.isDelivery, '1004')}</span>;
            }
          },

        ]
      },
      formChangeHandle(val) {
        this.saveData = val;
      },
      async saveDialog(type) {
        let submitData = this.$refs.table.$refs.pageTable.list;  //获取表格数据
        let rowsData = []
        console.info(submitData)
        submitData.forEach(item => {
          if (item.isDelivery === 10041002) {
            item.reportDate =  moment(this.rowData.reportDate).format('YYYY-MM-DD HH:mm:ss')
            rowsData.push(item)
          }
        })
        console.info(rowsData)
        if (rowsData.length === 0){
          this.$notify.error({title: '提示信息', message: '故障信息都已传输，无需再次传输！'});
          this.closeDialog('refresh')
        }else{
          let str = '';
          rowsData.map(x => {
            str += x.id + ',';
          });
          str = str.substring(0,str.length-1)
          console.log(str);
          const res = await transmissionData(rowsData);
          const res2 = await updateIsDelivery({ ids: str });
          if (res.resultCode === 200 && res2.resultCode === 200){
            this.closeDialog('refresh')
          }
        }
      },
      closeDialog(param) {
        this.$emit('close', {visible: false, type: param});
      }
    }
  }
</script>

<style scoped>

</style>
